#!/bin/sh
# PCP QA Test No. 1576
# pmlogger_daily - options from environment
# ... xz version for compression, based on No. 623
#
# Copyright (c) 2018 Ken McDonell.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

if which xz >/dev/null 2>&1
then
    if xz -0 --block-size=10MiB </dev/null >/dev/null 2>&1
    then
	PROG="xz -0 --block-size=10MiB"
    else
	PROG="xz"
    fi
    SUFF=xst
else
    _notrun "cannot find a zstd compression program!"
fi

NEEDREWRITE=$PCP_LOG_DIR/pmlogger/.NeedRewrite

_cleanup()
{
    cd $here
    $sudo rm -rf $tmp $tmp.*
    [ -f $NEEDREWRITE.$seq ] && $sudo mv $NEEDREWRITE.$seq $NEEDREWRITE
}

DATE=`pmdate -3d '%Y%m%d'`

_setup_control()
{
    echo '$version=1.1' >$tmp.control 
    for arg
    do
	echo "\$$arg" >>$tmp.control
    done
    echo "LOCALHOSTNAME	n   n	$tmp	$tmp.config" >>$tmp.control
    cat $tmp.control >>$seq_full
}

_setup()
{
    rm -f $tmp/* $tmp.log
    pmlogcp archives/ok-mv-bigbin $tmp
    for arch in $tmp/ok-mv-bigbin.*
    do
	touch -t ${DATE}0102 $arch
	mv $arch `echo $arch | sed -e "s/ok-mv-bigbin/${DATE}.01.02/"`
    done
}

_filter()
{
    # Note on sed ... need to deal with
    # Warning: -X value (no-such-compression-tool) ignored because $PCP_COMPRESS (xz ...) set in environment
    # Warning: $PCP_COMPRESS (xz ...) reset from control file, previous value (foo) ignored
    #

    if [ $# -eq 0 ]
    then
	cmd=cat
    elif [ -f "$1" ]
    then
	cmd="cat \"$1\""
    else
	return
    fi
    eval $cmd \
    | tee -a $seq_full \
    | sed \
	-e '/pmlogger_daily.pid: Permission denied/d' \
	-e '/no pmlogger instance running for host/d' \
	-e '/^pmlogger_daily:.*\.control:[0-9]]$/d' \
	-e '/logging for host .* unchanged/d' \
	-e '/^rm: cannot remove .*pmlogger_daily.stamp/d' \
	-e '/^cp: cannot create regular file .*pmlogger_daily.stamp/d' \
	-e '/^cp: .*\/pmlogger_daily.stamp: Permission denied/d' \
	-e '/^.[r-][w-][x-].*pmlogger_daily.stamp/d' \
	-e '/^rm: cannot remove .*pmlogger_daily.pid/d' \
	-e '/^_save_prev_filename: unable to remove/d' \
	-e '/^Warning: cannot save previous date-and-timestamp/d' \
	-e '/^Warning: cannot install new date-and-timestamp/d' \
	-e '/^Start: /d' \
	-e '/^End[: ]/d' \
	-e 's/^\(Warning:.*PCP_COMPRESS (xz\)\([^)]*\))/\1 ...)/' \
    # end
}

_showfiles()
{
    ls $tmp/* \
    | sed \
	-e "s@$tmp@TMP@" \
	-e "s/\.$SUFF$/.compressed/" \
	-e "s/$DATE/DATE/" \
    # end
}

status=1	# failure is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

[ -f $NEEDREWRITE ] && $sudo mv $NEEDREWRITE $NEEDREWRITE.$seq

mkdir $tmp

echo "log mandatory on default { pmcd }" >$tmp.config

# real QA test starts here

echo "simple cmd args" | tee -a $seq_full
_setup_control
_filter $tmp.log
_setup
pmlogger_daily -P -D -f -X "$PROG" -x 1 -c $tmp.control -l $tmp.log >$tmp.out 2>$tmp.err
_filter $tmp.err; _filter $tmp.out; _filter $tmp.log
_showfiles

echo | tee -a $seq_full
echo "simple env vars" | tee -a $seq_full
_setup
PCP_COMPRESS="$PROG" PCP_COMPRESSAFTER=1 pmlogger_daily -P -D -f -c $tmp.control -l $tmp.log >$tmp.out 2>$tmp.err
_filter $tmp.err; _filter $tmp.out; _filter $tmp.log
_showfiles

echo | tee -a $seq_full
echo "some error cases" | tee -a $seq_full
_setup
pmlogger_daily -P -D -f -x bozo -c $tmp.control -l $tmp.log >$tmp.out 2>$tmp.err
_filter $tmp.err; _filter $tmp.out; _filter $tmp.log
PCP_COMPRESSAFTER=bozo pmlogger_daily -P -D -f -c $tmp.control -l $tmp.log >$tmp.out 2>$tmp.err
_filter $tmp.err; _filter $tmp.out; _filter $tmp.log
pmlogger_daily -P -D -f -x 1 -X no-such-compression-tool -c $tmp.control -l $tmp.log >$tmp.out 2>$tmp.err
_filter $tmp.err; _filter $tmp.out; _filter $tmp.log
PCP_COMPRESS=no-such-compression-tool pmlogger_daily -P -D -f -x 1 -c $tmp.control -l $tmp.log >$tmp.out 2>$tmp.err
_filter $tmp.err; _filter $tmp.out; _filter $tmp.log

_setup_control PCP_COMPRESSAFTER=bozo
pmlogger_daily -P -D -f -c $tmp.control -l $tmp.log >$tmp.out 2>$tmp.err
_filter $tmp.err; _filter $tmp.out; _filter $tmp.log

echo | tee -a $seq_full
echo "some warning cases" | tee -a $seq_full
_setup_control
_setup
PCP_COMPRESS="$PROG" PCP_COMPRESSAFTER=1 PCP_COMPRESSREGEX='\.index$' pmlogger_daily -P -D -f -X no-such-compression-tool -x 7 -Y xyz -c $tmp.control -l $tmp.log >$tmp.out 2>$tmp.err
_filter $tmp.err; _filter $tmp.out; _filter $tmp.log
_showfiles
_setup_control "PCP_COMPRESS='$PROG'" PCP_COMPRESSAFTER=1 "PCP_COMPRESSREGEX='\.(index|meta)$'"
_setup
PCP_COMPRESS=foo PCP_COMPRESSAFTER=42 PCP_COMPRESSREGEX='\.bar$' pmlogger_daily -P -D -f -c $tmp.control -l $tmp.log >$tmp.out 2>$tmp.err
_filter $tmp.err; _filter $tmp.out; _filter $tmp.log
_showfiles

# success, all done
status=0
exit
